package com.edu.bhu.admin.controller.admin;

import java.util.List;
import java.util.stream.Collectors;

import com.edu.bhu.admin.domain.PreClass;
import com.edu.bhu.admin.domain.PreDetection;
import com.edu.bhu.admin.domain.PreRecord;
import com.edu.bhu.admin.service.IPreClassService;
import com.edu.bhu.admin.service.IPreDetectionService;
import com.edu.bhu.admin.service.IPreRecordService;
import com.edu.bhu.common.annotation.Log;
import com.edu.bhu.common.core.controller.BaseController;
import com.edu.bhu.common.core.domain.AjaxResult;
import com.edu.bhu.common.core.domain.entity.SysUser;
import com.edu.bhu.common.core.page.TableDataInfo;
import com.edu.bhu.common.enums.BusinessType;
import com.edu.bhu.common.utils.ShiroUtils;
import com.edu.bhu.common.utils.poi.ExcelUtil;
import com.edu.bhu.system.service.ISysUserService;
import io.swagger.v3.oas.models.security.SecurityScheme;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

/**
 * 班级Controller
 *
 * @author yutianyang
 * @date 2022-04-06
 */
@Controller
@RequestMapping("/admin/class")
public class PreClassController extends BaseController
{
    private String prefix = "admin/class";

    @Autowired
    private IPreClassService preClassService;

    @Autowired
    private ISysUserService userService;

    @Autowired
    private IPreDetectionService preDetectionService;

    @Autowired
    private IPreRecordService preRecordService;

    @RequiresPermissions("admin:class:view")
    @GetMapping()
    public String classes()
    {
        return prefix + "/class";
    }

    /**
     * 我的班级页面
     */
    @RequiresPermissions("admin:class:view")
    @GetMapping("/myClass")
    public String myClass()
    {
        return prefix + "/myClass";
    }

    /**
     * 查询某班级的用户列表
     */
    @RequiresPermissions("admin:class:list")
    @PostMapping("/myClassUserlist")
    @ResponseBody
    public TableDataInfo myClassUserlist(SysUser user)
    {
        user.setClassId(ShiroUtils.getSysUser().getClassId());//筛选班级
        Long userId = ShiroUtils.getSysUser().getUserId();
        startPage();
        List<SysUser> list = userService.selectUserList(user);
        list = list.stream().filter(m ->
                m.getUserId() != Long.valueOf(userId)
        ).map(m -> {
            //添加途径地记录（14天内）
            PreRecord preRecord = new PreRecord();
            preRecord.setUserId(m.getUserId());
            preRecord.setStatus(2L);
            Integer i = preRecordService.selectDangerCount(preRecord);
            preRecord.setStatus(3L);
            Integer j = preRecordService.selectDangerCount(preRecord);
            preRecord.setStatus(4L);
            Integer k = preRecordService.selectDangerCount(preRecord);

            Long sta = 1L;
            String str = "途经";
            if(i > 0){
                str += "低风险、";
            }
            if(j > 0){
                str += "中风险、";
            }
            if(k > 0){
                str += "高风险、";
            }
//            int indx = str.lastIndexOf("、");
//            if(indx!=-1){
//                str = str.substring(0,indx)+str.substring(indx+1,str.length());
//            }
            if(i==0 && j==0 && k==0){
                sta = 0L;
            }
            m.setWayRecordStatusStr(str);
            m.setWayRecordStatus(sta);

            //添加核酸检测记录（14天内）
            PreDetection preDetection = new PreDetection();
            preDetection.setUserId(m.getUserId());
            Integer l = preDetectionService.selectDangerCount(preDetection);
            if(l > 0){
                m.setDetectionStatus(2L);//有阳性记录！
            } else{
                m.setDetectionStatus(1L);//正常
            }
            return m;
        }).collect(Collectors.toList());
        return getDataTable(list);
    }

    /**
     * 查询班级列表
     */
    @RequiresPermissions("admin:class:list")
    @PostMapping("/list")
    @ResponseBody
    public TableDataInfo list(PreClass preClass)
    {
        startPage();
        List<PreClass> list = preClassService.selectPreClassList(preClass);
        return getDataTable(list);
    }

    /**
     * 查询班级列表（不分页）
     */
    @RequiresPermissions("admin:class:list")
    @PostMapping("/listNotPage")
    @ResponseBody
    public TableDataInfo listNotPage(PreClass preClass)
    {
        List<PreClass> list = preClassService.selectPreClassList(preClass);
        return getDataTable(list);
    }

    /**
     * 导出班级列表
     */
    @RequiresPermissions("admin:class:export")
    @Log(title = "班级", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    @ResponseBody
    public AjaxResult export(PreClass preClass)
    {
        List<PreClass> list = preClassService.selectPreClassList(preClass);
        ExcelUtil<PreClass> util = new ExcelUtil<PreClass>(PreClass.class);
        return util.exportExcel(list, "班级数据");
    }

    /**
     * 新增班级
     */
    @GetMapping("/add")
    public String add()
    {
        return prefix + "/add";
    }

    /**
     * 新增保存班级
     */
    @RequiresPermissions("admin:class:add")
    @Log(title = "班级", businessType = BusinessType.INSERT)
    @PostMapping("/add")
    @ResponseBody
    public AjaxResult addSave(PreClass preClass)
    {
        return toAjax(preClassService.insertPreClass(preClass));
    }

    /**
     * 修改班级
     */
    @RequiresPermissions("admin:class:edit")
    @GetMapping("/edit/{classId}")
    public String edit(@PathVariable("classId") Long classId, ModelMap mmap)
    {
        PreClass preClass = preClassService.selectPreClassByClassId(classId);
        mmap.put("preClass", preClass);
        return prefix + "/edit";
    }

    /**
     * 修改保存班级
     */
    @RequiresPermissions("admin:class:edit")
    @Log(title = "班级", businessType = BusinessType.UPDATE)
    @PostMapping("/edit")
    @ResponseBody
    public AjaxResult editSave(PreClass preClass)
    {
        return toAjax(preClassService.updatePreClass(preClass));
    }

    /**
     * 删除班级
     */
    @RequiresPermissions("admin:class:remove")
    @Log(title = "班级", businessType = BusinessType.DELETE)
    @PostMapping( "/remove")
    @ResponseBody
    public AjaxResult remove(String ids)
    {
        return toAjax(preClassService.deletePreClassByClassIds(ids));
    }

    /**
     * 修改保存班级
     */
    @RequiresPermissions("admin:class:edit")
    @Log(title = "班级", businessType = BusinessType.UPDATE)
    @PostMapping("/memRemove")
    @ResponseBody
    public AjaxResult memRemove(SysUser sysUser)
    {
        sysUser.setClassId(0L);
        return toAjax(userService.changeStatus(sysUser));
    }

    /**
     * 途径地记录
     */
    @GetMapping("/wayModel/{userId}")
    public String wayModel(@PathVariable("userId") Long userId, ModelMap mmap)
    {
        mmap.put("userId", userId);
        return prefix + "/wayModel";
    }

    /**
     * 身体状况记录
     */
    @GetMapping("/bodyModel/{userId}")
    public String bodyModel(@PathVariable("userId") Long userId, ModelMap mmap)
    {
        mmap.put("userId", userId);
        return prefix + "/bodyModel";
    }

    /**
     * 核酸检测记录
     */
    @GetMapping("/detectionModel/{userId}")
    public String detectionModel(@PathVariable("userId") Long userId, ModelMap mmap)
    {
        mmap.put("userId", userId);
        return prefix + "/detectionModel";
    }

    /**
     * 疫苗接种记录
     */
    @GetMapping("/vaccinationModel/{userId}")
    public String vaccinationModel(@PathVariable("userId") Long userId, ModelMap mmap)
    {
        mmap.put("userId", userId);
        return prefix + "/vaccinationModel";
    }

}
